// 2013-8-31下午02:30:21  net.betou.trade.main.manager.impl.PictureMngImpl.java

package net.betou.trade.main.manager.impl;

import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Picture;
import net.betou.trade.main.dao.PictureDao;
import net.betou.trade.main.manager.PictureMng;
import net.betou.trade.main.manager.UserMng;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class PictureMngImpl implements PictureMng{

	public Picture deleteById( Integer id ) {
		Picture p = dao.findById( id );
		return dao.delete( p );
	}

	public Picture [ ] deleteByIds( Integer [ ] ids ) {
		Picture [ ] pics = new Picture[ ids.length ];
		
		int i = 0;
		for( Integer id : ids ){
			Picture p = deleteById( id );
			pics[ i ] = p;
			i++;
		}
		
		return pics;
	}
	
	@Transactional( readOnly = true )
	public Picture findById( Integer id ) {
		return dao.findById( id );
	}

	public Picture save( AgentAndAdmins admin, String path, Integer userId ) {
		Picture p = new Picture( );
		p.setPath( path );
		
		if( userId != null ){
			p.setUser( userMng.findById( userId ) );
		}else{
			p.setAdmin( admin );
		}
		
		p.init( );
		
		return dao.save( p );
	}

	@Autowired
	private PictureDao dao;
	
	@Autowired
	private UserMng userMng;

}
